#! /usr/bin/env python3
# coding=utf-8
import xlrd
import xlwt

def excel_read(path_file='./file/test.xls',sheet='Sheet1'):
	xlbook=xlrd.open_workbook(path_file)  #打开excel
	count=len(xlbook.sheets())            #获取excel工作簿数
	table = xlbook.sheet_by_name(sheet)   # 通过名称获取工作表
	nrows = table.nrows  # 获取行数
	ncols = table.ncols  # 获取列数
	print("工作簿数：%d 当前工作表行数:%d 当前工作表列数:%d"% (count,nrows,ncols))
	# 按行循环读取数据，保存到列表
	list = []
	for i in range(1, nrows):#从索引1开始，去除第一行
		list.append(table.row_values(i))
	return list

def excel_alter(list):
	for i in range(len(list)):
		if list[i][2] == 123:
			list[i][2] = 188
			print(list[i])
		else:
			print(list[i])
	return list

def excel_write(list,path_file='./file/new.xls',sheet='new'):#没有默认值的参数需放前面
	wbk=xlwt.Workbook(encoding = 'ascii')
	wlbook=wbk.add_sheet(sheet)
	#写入标题栏
	title=['sn','account','password','name']
	for t in range(len(title)):
		wlbook.write(0,t,title[t],style())
	#写入列表数据
	for i in range(1,len(list)+1):
		for j in range(len(list[i-1])):
			wlbook.write(i,j,list[i-1][j])
	wbk.save(path_file)

def style():
	font = xlwt.Font()  # Create the Font
	font.name = 'Times New Roman'
	font.bold = True
	font.underline = True
	font.italic = True
	style = xlwt.XFStyle()  # Create the Style
	style.font = font       # Apply the Font to the Style
	return style

a=excel_read()
b=excel_alter(a)
excel_write(b)

